package com.symria.system.email;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;



/**
 * 功能描述: 邮件发送帮助类
 * @author 罗有华
 * @date Jul 7, 2010
 */
public class EmailUtil {
	private ExecutorService service;//线程池
	private static EmailSender sender; //发送邮件工具默认发送Esmtp
	private String subject;//邮件名称
	private String content;//邮件内容
	private File[] files;//附件
	private String from;//发送邮件
	private List<String> list; //收信人list(量比较大的时候采用线程发送)
	private int step = 500; //量大的时候默认每次发送500封
	private String to; //单个收信人
	private boolean isTemplate = false;//模板邮件
	private String path ;//模板路径
	private Map<Object, Object> map;//模板数据
    	
	EmailUtil(){}
	//初始化from
	public EmailUtil(String from){
		this.from = from;
	}
    public static EmailUtil createInstance(String from){
    	sender = EmailExecute.createESmtpMailSender(from);
		return new EmailUtil(from);
    }
    //设置日志处理类
	public void setLogger(LogManager log){
		sender.addLogManager(log);
	}
	//发送esmtp邮件
	public void createESmtpMailSender(){
		sender = EmailExecute.createESmtpMailSender(from);
	}
	//启动自定义 stmp user pwd发送邮件
	public void createESmtpMailSender(String stmp,String user,String pwd){
		sender = EmailExecute.createESmtpMailSender(from, stmp, user, pwd);
	}
	
	//发送smtp邮件
	public void createSmtpMailSender(){
		sender = EmailExecute.createSmtpMailSender(from);
	}
	//给单个人发 html形式
	public boolean sendHtmlEmail(String to,String subject,String content){
		if(isTemplate)  content = this.getTemplateEmail(path, map);
		return sender.sendHtmlMail(to, subject, content);
	}
	//给多个人发 html形式
	public void sendHtmlEmail(String[] to,String subject,String content){
		if(isTemplate)  content = this.getTemplateEmail(path, map);
		sender.sendHtmlMail(to, subject, content);
	}
	//给单个人发 html形式 带attachments是附件
	public void sendHtmlEmail(String to,String subject,String content,File[] attachments){
		if(isTemplate)  content = this.getTemplateEmail(path, map);
		sender.sendHtmlMail(to, subject, content, attachments);
	}
	//给多个人发 html形式 带附件
	public void sendHtmlEmail(String[] to,String subject,String content,File[] attachments){
		if(isTemplate)  content = this.getTemplateEmail(path, map);
		sender.sendHtmlMail(to, subject, content, attachments);
	}
	//给单个人发文本形式
	public boolean sendTextEmail(String to,String subject,String content){
	    return	sender.sendTextMail(to, subject, content);
		
	}
	//给多个人发文本形式
	public boolean[] sendTextEmail(String[] to,String subject,String content){
		return sender.sendTextMail(to, subject, content);
	}
	//给单个人发 文本形式 带attachments是附件
	public boolean sendTextEmail(String to,String subject,String content,File[] attachments){
	    return	sender.sendTextMail(to, subject, content, attachments);
	}
	//给多个人发 文本形式 带attachments是附件
	public boolean[] sendTextEmail(String[] to,String subject,String content,File[] attachments){
		return sender.sendTextMail(to, subject, content, attachments);
	}
	
	public void newThreadSendEmail(String email,String subject,String content){
		List<String> l = new ArrayList<String>();
		l.add(email);
		if(isTemplate)  content = getTemplateEmail(path, map);
		newThreadSendEmail(l, from, subject, content, null);
	}
	//启用多线程发送邮件
	private void newThreadSendEmail(List<String> list,String from,String subject,String content,String type){
		if(list != null){
			service = Executors.newCachedThreadPool();
			if(isTemplate) content = getTemplateEmail(path, map);
			while(list.size()!=0){
				service.submit(new EmailSenderRunnable(popList(list,step<=0?20:step),sender,subject,from,content,files,type));
				try {
					Thread.sleep(30);//睡30毫秒 继续
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
			}
			service.shutdown();
		}
	}
	//启用多线程发送html邮件
	public void newTheardSendhtmlEmail(List<String> list,String subject,String content){
		newThreadSendEmail(list,from,subject,content,null);
	}
	public void newTheardSendtextEmail(List<String> list,String subject,String content){
		newThreadSendEmail(list,from,subject,content,"TEXT");
	}
	//从list中取出num个邮件地址
	public  List<String> popList(List<String> list,int num){
		  List<String> l = new ArrayList<String>();
		  for(int i=0;i<num;i++){
			  if(list.size()==0) break;
			  l.add(list.get(0));
			  list.remove(0);
		  }
		  return l;
	  }
	//设置邮件内容从模板里面去取
	public void setTemplateEmail(String path,Map<Object, Object> map){
		this.isTemplate = true;
		this.map= map;
		this.path = path;
	}
	//取模板数据
	private String getTemplateEmail(String path,Map<Object, Object> map){
		String body = FreemarkerUtil.getInstance("/template").process(path, map);
		return body;
	}
	public String getSubject() {
		return subject;
	}
	public void setSubject(String subject) {
		this.subject = subject;
	}
	public String getContent() {
		return content;
	}
	public void setContent(String content) {
		this.content = content;
	}
	
	public String getFrom() {
		return from;
	}
	public void setFrom(String from) {
		this.from = from;
	}
	public List<String> getList() {
		return list;
	}
	public void setList(List<String> list) {
		this.list = list;
	}
	public int getStep() {
		return step;
	}
	public void setStep(int step) {
		this.step = step;
	}
	public String getTo() {
		return to;
	}
	public void setTo(String to) {
		this.to = to;
	}
	public boolean isTemplate() {
		return isTemplate;
	}
	public void setTemplate(boolean isTemplate) {
		this.isTemplate = isTemplate;
	}
	public String getPath() {
		return path;
	}
	public void setPath(String path) {
		this.path = path;
	}
	public Map<Object, Object> getMap() {
		return map;
	}
	public void setMap(Map<Object, Object> map) {
		this.map = map;
	}

	/**=======================测试代码================================*/
	
	public static void main(String args[]){
		test1();
		//test2();
	}

	/**发送普通邮件*/
	public static void test1(){
		String from = "\"lyh\"<luoyouhua@widu.com.cn>";
		EmailUtil util = EmailUtil.createInstance(from);
		util.setLogger(new EmailLogger());		
		util.sendTextEmail("\"widu\"<fujianyd@163.com>", "keyword for widu", "teafsafsafsdfa!");
	}
	/**发送html邮件*/
	public static void test2(){
		String from = "\"lyh\"<blackfox@gameeden.org>";
		EmailUtil util = EmailUtil.createInstance(from);
		util.setLogger(new EmailLogger());
		util.sendHtmlEmail("\"Sol\"<fujianyd@tom.com>", "邮件测试", "<span style='color:red'>发送邮件测试</span>");
	}
	/**开线程*/
	public static void test3(){
		String from = "\"lyh\"<fjh@wooboo.com.cn>";
		EmailUtil util = EmailUtil.createInstance(from);
		util.createESmtpMailSender("mail.wooboo.com.cn", "fjh@wooboo.com.cn", "Wooboo(678)");
		List<String> list = new ArrayList<String>();//getList(10060);//定义一万封邮箱
		list.add("\"lyh\"<fujianyd@tom.com>");
		list.add("\"lyh\"<fujianyd@163.com>");
		util.setStep(1000);//每一千封一个thread
		util.setLogger(new EmailLogger());
		util.newTheardSendhtmlEmail(list, "邮件测试多线程", "duo xiancheng");
	}
	/**发送模板邮件 在action中运行*/
	public static void test4(){
		String from = "\"lyh\"<blackfox@gameeden.org>";
		EmailUtil util = new EmailUtil(from);
		util.createESmtpMailSender();
		Map map = new HashMap();
		map.put("user", "luoyouhua");
		util.setTemplateEmail("/email/success.html", map);
		util.sendHtmlEmail("\"lyh\"<fujianyd@tom.com>", "html", null);
	}
	private static int num = 0;
	private static List<String> getList(int n){
		  List<String> l = new ArrayList<String>();
		  for(;n>0;n--){
			  num++;
			  l.add("fujianyd@tom.com"+num);
		  }
		  return l;
	}
}
